/**************************************
 * This file contains the definitions *
 * for the Window Class.              *
 **************************************/

#include "Window.h"

//Callback Function Definition
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		case WM_DESTROY:
			{
				PostQuitMessage (WM_QUIT);
			} break;
		default:
			return DefWindowProc (hWnd, uMsg, wParam, lParam);
	}
	return 0;
}

//////////////////////////////
// Window Class Definitions //
//////////////////////////////

//Constructors
Window::Window (void)
{
}

Window::Window (HINSTANCE Instance, LPCWSTR Title, int Width, int Height, BOOL Fullscreen)
{
	InitWindow (Instance, Title, Width, Height, Fullscreen);
}

//Destructor
Window::~Window (void) 
{
	if (WndClassEx != NULL)
		delete WndClassEx;
}

BOOL Window::InitWindow (HINSTANCE Instance, LPCWSTR Title, int Width, int Height, BOOL Fullscreen)
{
	hInstance = Instance;
	WindowTitle = Title;
	ScreenWidth = Width;
	ScreenHeight = Height;
	FullScreen = Fullscreen;
	return BuildWindow();
}

//Public Methods
void Window::Show (void)
{
	ShowWindow (hWnd, SW_SHOWNORMAL);
	UpdateWindow (hWnd);
}

BOOL Window::UpdateWindowMsg (void)
{
	if (PeekMessage (&Msg, NULL, 0, 0, PM_REMOVE))
	{
		if (Msg.message == WM_QUIT)
			Running = FALSE;
		TranslateMessage (&Msg);
		DispatchMessage (&Msg);
	}
	return Running;
}

//Private Methods
BOOL Window::BuildWindow (void)
{
	BOOL Success = TRUE;
	Success = BuildWndClassEx();
	if (Success)
		Success = BuildWndHandle();
	return Success;
}

BOOL Window::BuildWndClassEx (void)
{
	BOOL Success = TRUE;

	WndClassEx = new WNDCLASSEX;
	if (WndClassEx)
	{
		WndClassEx->cbSize			= sizeof (WNDCLASSEX);
		WndClassEx->style			= CS_HREDRAW | CS_VREDRAW;
		WndClassEx->lpfnWndProc		= WindowProcedure;
		WndClassEx->cbClsExtra		= 0;
		WndClassEx->cbWndExtra		= 0;
		WndClassEx->hInstance		= hInstance;
		WndClassEx->hIcon			= LoadIcon (NULL, IDI_APPLICATION);
		WndClassEx->hIconSm			= LoadIcon (NULL, IDI_APPLICATION);
		WndClassEx->hCursor			= LoadCursor (NULL, IDC_ARROW);
		WndClassEx->hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
		WndClassEx->lpszMenuName	= NULL;
		WndClassEx->lpszClassName	= WIN_CLS_NAME;

		if (!RegisterClassEx(WndClassEx))
			Success = FALSE;
	}
	else
		Success = FALSE;
	
	return Success;
}

BOOL Window::BuildWndHandle (void)
{
	BOOL Success = TRUE;

	if (FullScreen)
	{
		hWnd = CreateWindowEx(NULL, WIN_CLS_NAME, WindowTitle, WS_EX_TOPMOST | WS_POPUP,
							  0, 0, ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL);
	}
	else
	{
		hWnd = CreateWindowEx (WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, WIN_CLS_NAME, WindowTitle,
							   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, ScreenWidth,
							   ScreenHeight, NULL, NULL, hInstance, NULL);
	}
	if (!hWnd)
		Success = FALSE;

	return Success;
}

//Getters
HINSTANCE Window::GetWindowInstance (void)
{
	return hInstance;
}

HWND Window::GetWindowHandle (void)
{
	return hWnd;
}

UINT Window::GetScreenWidth (void)
{
	return ScreenWidth;
}

UINT Window::GetScreenHeight (void)
{
	return ScreenHeight;
}